'''
Created on 9/03/2013

@author: Jorge
'''
from BinormalSeparation import BinormalSeparation
from FeatureSelector import FeatureSelector
import random

class BinormalSeparationWithRandRoubin(FeatureSelector):
    '''
    classdocs
    '''


    def __init__(self,num_features):
        '''
        Constructor
        '''
        self.num_features = num_features
        
    def set_num_features(self, num_features):
        self.num_features = num_features
        
    def get_index_features(self, X, Y):
        
        print "num features original: ", len(X[0])
        indexes_features = []
        classes = self.__create_group(X, Y)
        c = classes.keys()
        features_per_class = []
        prob_per_clas = []
        print 'numero de clases', len(c)
        featureSelector = BinormalSeparation()
        for current in c:
            rest_class = classes.keys()
            rest_class.remove(current)
            rest = []
            for list_claims in [classes[t] for t in rest_class ]:
                for x in list_claims:
                    rest.append(x)
                    
            one_vs_all_X =  [x for x in classes[current] ] + rest
            one_vs_all_Y = [current]*len(classes[current]) + ['rest']*len(rest)
            features = featureSelector.get_feature_score(one_vs_all_X, one_vs_all_Y)
            features.sort()
            print "features len ", len(features)
            features_per_class.append( features )
            prob_per_clas.append( 1.0/len(classes[current]) )
        
        prob_per_clas = [prob/sum(prob_per_clas) for prob in prob_per_clas ]
        interval = prob_per_clas
        for i in range(1, len(interval)):
            interval[i] += interval[i-1]
        print "interval: ",interval
        
        k=0
        while k< self.num_features:
            k+=1
            numRandom = random.random()
            for i in range(len(interval)):
                #print i
                if (0 if i==0 else interval[i-1]) <= numRandom <= interval[i] :
                    temp = None
                    flag=True
                    while flag==True or temp in indexes_features:
                        if len(features_per_class[i])==0:
                            prob_per_clas.pop(i)
                            prob_per_clas = [prob/sum(prob_per_clas) for prob in prob_per_clas ]
                            interval = prob_per_clas
                            for i in range(1, len(interval)):
                                interval[i] += interval[i-1]
                            print "new interval: ",interval
                            k-=1
                            temp = None
                            break
                        else:
                            temp = features_per_class[i].pop().index
                            flag=False
                    if temp!=None:
                        indexes_features.append(temp)
                    break
        print "num features", len(indexes_features) 
        print "features: ",indexes_features  
        return indexes_features
        
        
    
    def __create_group(self, X,Y):
        m = len(Y)
        group = {}
        for i in range(m):
            try:
                group[Y[i]].append(X[i])
            except KeyError:
                group[Y[i]] = [X[i]]
        return group